//
//  WordSummaryListCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/7/2.
//

#import "WordSummaryListCell.h"
#import <SDWebImage/UIImage+GIF.h>

@interface WordSummaryListCell()

@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *analyzeLabel;
@property(nonatomic,strong)UIButton *playBtn;
@property(nonatomic,strong)UIImageView *playImageView;

@end

@implementation WordSummaryListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    __weak typeof(self)self_weak_ = self;
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.font = JPUIFontNotoSansJPRegular(16);
    self.nameLabel.textColor = KFontBlackColor;
    self.nameLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@0);
        make.centerY.mas_equalTo(self_weak_.contentView).offset(-14);
        make.height.mas_equalTo(@22);
        make.right.mas_equalTo(@-30);
    }];
    
    UIView *imageBgView = [[UIView alloc]init];
    imageBgView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:imageBgView];
    [imageBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@5);
        make.width.height.mas_equalTo(@30);
        make.centerY.mas_equalTo(self_weak_.nameLabel);
    }];
    UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if(self_weak_.playBlock){
            self_weak_.playBlock();
        }
    }];
    [imageBgView addGestureRecognizer:imageTap];
    
    self.playImageView = [[UIImageView alloc]init];
    self.playImageView.userInteractionEnabled = YES;
    self.playImageView.image = [UIImage imageNamed:@"mint_mycollect_play"];
    [imageBgView addSubview:self.playImageView];
    [self.playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@20);
        make.centerY.mas_equalTo(imageBgView);
        make.centerX.mas_equalTo(imageBgView);
    }];
    
    self.analyzeLabel = [[UILabel alloc]init];
    self.analyzeLabel.font = JPUIFontPingFangSCRegular(13);
    self.analyzeLabel.textColor = KFontGrayColor;
    self.analyzeLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.analyzeLabel];
    [self.analyzeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(@0);
        make.height.mas_equalTo(@18);
        make.top.mas_equalTo(self_weak_.nameLabel.mas_bottom).offset(10);
    }];
    
}

-(void)setWordModel:(WordListModel *)wordModel{
    _wordModel = wordModel;
    if(wordModel.tone&&![wordModel.tone isEqualToString:@"(null)"]){
        self.nameLabel.text = [NSString stringWithFormat:@"%@%@",wordModel.writing,wordModel.tone];
    }else{
        self.nameLabel.text = wordModel.writing;
    }
    self.analyzeLabel.text = wordModel.meaning;
}

#pragma mark - 开始播放动画
- (void)startVoiceAnimation{
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"voice_play.gif" ofType:nil];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    self.playImageView.image = [UIImage sd_imageWithGIFData:imageData];
}

#pragma mark - 结束播放动画
- (void)stopVoiceAnimation{
    self.playImageView.image = [UIImage imageNamed:@"mint_mycollect_play"];
}

@end
